<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>五指山污水处理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/css/public.css" media="all">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/css/logincss/layout.css" media="all">
    <link rel="shortcut icon" href="<%=request.getContextPath()%>/huazhi/images/favicon.ico"/>
</head>
<style>
    /* 底部固定区域 */
    .layui-layout-admin .layui-footer {
        padding: 10px 0;
        text-align: center;
    }

    .layui-layout-admin .layui-footer {
        background-color: #fff;
    }


</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" lay-filter="component-form-element" id="form" method="post" enctype="multipart/form-data">
                <input type="hidden" name="tid" id="tid">
                <div class="layui-tab layui-tab-brief">
                    <ul class="layui-tab-title">
                        <li class="layui-this">基本信息</li>
                    </ul>
                    <div class="layui-tab-content" style="height: auto">
                        <div class="layui-tab-item layui-show">
                            <div class="layui-form-item">
                                <div class="layui-col-lg6">
                                    <div class="layui-col-xs11">
                                        <label class="layui-form-label">养护日期</label>
                                        <div class="layui-input-block">
                                            <input name="maintaindate" lay-verify="maintaindate" class="layui-input" id="maintaindate"
                                                   placeholder="请选择养护日期" type="text">
                                        </div>
                                    </div>
                                    <div class="layui-col-xs1">
                                        <div class="layui-form-mid layui-word-aux"
                                             style="color: red !important;padding: 10px !important;">*
                                        </div>
                                    </div>
                                </div>
                            </div>
<%--                            <div class="layui-form-item">--%>
<%--                                <div class="layui-col-lg6">--%>
<%--                                    <div class="layui-col-xs11">--%>
<%--                                        <label class="layui-form-label">时间</label>--%>
<%--                                        <div class="layui-input-block">--%>
<%--                                            <input name="time" lay-verify="time" class="layui-input" id="time"--%>
<%--                                                   placeholder="请选择时间" type="text">--%>
<%--                                        </div>--%>
<%--                                    </div>--%>
<%--                                    <div class="layui-col-xs1">--%>
<%--                                        <div class="layui-form-mid layui-word-aux"--%>
<%--                                             style="color: red !important;padding: 10px !important;">*--%>
<%--                                        </div>--%>
<%--                                    </div>--%>
<%--                                </div>--%>
<%--                            </div>--%>
                            <div class="layui-form-item">
                                <div class="layui-col-lg6">
                                    <div class="layui-col-xs11">
                                        <label class="layui-form-label">村名:</label>
                                        <div class="layui-input-block">
                                            <div class="layui-form" lay-filter="village">
                                                <select name="naturalname" id="village" lay-verify="cun" lay-filter="villageselect" lay-search>
                                                    <option value="">请选择</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="layui-col-xs1">
                                        <div class="layui-form-mid layui-word-aux"
                                             style="color: red !important;padding: 10px !important;">*
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-col-lg6">
                                    <div class="layui-col-xs11">
                                        <label class="layui-form-label">设施编号</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="devicenum"
                                                   autocomplete="off" placeholder="请输入设施编号"
                                                   class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-col-lg6">
                                    <div class="layui-col-xs11">
                                        <label class="layui-form-label">养护内容</label>
                                        <div class="layui-input-block">
                                            <textarea class="layui-textarea" name="maintainproject"
                                                      placeholder="请输入养护内容"></textarea>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-col-lg6">
                                    <div class="layui-col-xs11">
                                        <label class="layui-form-label">养护后状况</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="maintainstate"
                                                   autocomplete="off" placeholder="请输入养护后的状况"
                                                   class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-col-lg6">
                                    <div class="layui-col-xs11">
                                        <label class="layui-form-label">养护人员</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="maintainuser"
                                                   autocomplete="off" placeholder="请输入养护人员"
                                                   class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-col-lg6">
                                    <div class="layui-col-xs12" id="image">
                                        <label class="layui-form-label">养护图片</label>
                                        <span class="layui-btn layui-btn-primary">选择图片
                                    <input type="file" id="uploadImg" class="uploadImg" name="uploadImage" multiple="multiple">
                                </span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-footer" style="left: 0;padding: 0px;">
                        <button class="layui-btn" lay-submit lay-filter="*" id="pushButton">确认修改</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/layui.all.js"></script>
<script src="<%=request.getContextPath()%>/huazhi/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="<%=request.getContextPath()%>/huazhi/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script>
    function child(tid){
        layui.use(['form', 'laydate', 'layer'], function () {
            var $ = layui.$
                , form = layui.form
                , laydate = layui.laydate
                , layer = layui.layer;
            laydate.render({
                elem:'#maintaindate',
                type:'datetime'
            });
            $.ajax({
                type: "get",
                url: "<%=request.getContextPath()%>/rest/base/getMaintainRecordByTid",
                dataType: "json",
                async: false,
                data:"tid="+tid,
                success: function (msg) {
                    $.ajax({
                        type: "get",
                        url: "<%=request.getContextPath()%>/rest/base/getSelectVillage",
                        dataType: "json",
                        async: false,
                        success: function (msg) {
                            var select = document.getElementById("village");
                            $.each(msg.data,function(i,n) {
                                select.options.add(new Option(n.name,n.tid));
                            });
                            form.render('select','village');
                        }
                    });
                    var pNode = document.getElementById("image");
                    var image = msg.image;
                    if(image != null && image !== ""){
                        var imagepath = image.split(",");
                        if(imagepath.length > 0){
                            for(var i = 0; i < imagepath.length; i++){
                                $("<div class='imglist'>\n" +
                                    "<img src='"+ imagepath[i] +"' id='img3' class='img3' alt=''/>\n" +
                                    "<div class='prompt3'></div>"+
                                    "</div>").insertAfter(pNode.children[0]);
                            }
                        }
                    }
                    form.val("component-form-element", msg)
                }
            });
            //自定义验证规则
            form.verify({
                maintaindate: function (value, item) {
                    if (value === "") {
                        return '请选择养护日期';
                    }
                },
                cun: function (value, item) {
                    if (value === "") {
                        return '请选择养护的村名称';
                    }
                },
            });

            //第一种方法添加用户
            form.on('submit(*)', function (data) {
                var field = data.field//当前容器的全部表单字段，名值对形式：{name: value}
                var formData = new FormData($("#form")[0]);
                var index = layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.6});
                $("#pushButton").addClass("layui-btn layui-btn-disabled").prop("disabled", true);
                $.ajax({
                    type: "post",
                    url: "<%=request.getContextPath()%>/rest/base/updateMaintainRecord",
                    data: formData,
                    processData: false,			//对数据不做处理
                    dataType: "json",
                    cache: false,      			//上传文件不需要缓存
                    contentType: false,
                    async:false,
                    success: function (obj) {
                        setTimeout(function () {
                            layer.close(index);
                            if (obj === true) {
                                layer.msg('修改成功');
                            } else {
                                layer.msg(obj.errorMsg);
                            }
                        }, 2000);
                        setTimeout(function () {
                            var indexs = parent.layer.getFrameIndex(window.name);//获取当前弹出层的层级
                            parent.layer.close(indexs);//关闭弹出层
//                        window.parent.location.reload();//刷新父页面
                        }, 4000);
                    }
                })
                return false;
            });
            $('#uploadImg').bind("change",function(){
                var pNode = document.getElementById("image");
                var fileData = this.files;
                var pettern = /^image/;
                if(fileData.length > 5 ){
                    layer.msg("最多只能上传五张图片", {icon: 5, anim: 6});
                    this.value = null;
                }else if(fileData.length > 0){
                    for(var j = pNode.children.length - 1; j > 0; j--){
                        if(pNode.children[j].className === "imglist"){
                            pNode.removeChild(pNode.children[j]);
                        }
                    }
                    for(var i = 0; i < fileData.length; i++){
                        var file = fileData[i];
                        if(pettern.test(file.type)){
                            if(file.size >=2097152){
                                layer.msg("图片"+file.name+"过大，请选择小于2MB的图片", {icon: 5, anim: 6});
                                this.value = null;
                            }else{
                                var reads = new FileReader();
                                reads.readAsDataURL(file);
                                reads.onload = function(e) {
                                    $("<div class='imglist'>\n" +
                                        "<img src='"+ this.result +"' id='img3' class='img3'/>\n" +
                                        "<div class='prompt3'></div>"+
                                        "</div>").insertAfter(pNode.children[0]);
                                };
                            }
                        }else {
                            layer.msg(file.name+"不是图片，请上传正确的图片文件", {icon: 5, anim: 6});
                            this.value = null;
                        }
                    }
                }else {
                    layer.msg("未选择任何文件", {icon: 5, anim: 6});
                }
            });

        });
    }
</script>
</body>
</html>